About 3536 letters

About 18 minutes

#Python's inheritance and polymorphism

Inheritance is a syntax for creating a new class based on an existing class. The inherited class is called the parent class, and the new class created is called the child class. The child class automatically inherits the same attributes and methods as the parent class.

class ClassName(ParentClassName): pass

The parent class is also called the base or superclass, and the child class is also called the derived class.

Example:

class Pet: """ Pet class """ def __init__(self, name: str): """ Constructor Args: name (str): Name of the pet """ self.__name = name def name(self) -> str: """ Get the pet's name Returns: str: The name of the pet """ return self.__name # Cat class inherits from Pet class Cat(Pet): # Additional method def catch_mouses(self): pass # Dog class inherits from Pet class Dog(Pet): # Additional method def guard_gates(self): pass # Create objects using inherited constructor tom = Cat("Tom") happy = Cat("Happy") spike = Dog("Spike") # Call inherited method print(tom.name()) print(happy.name()) print(spike.name())

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Polymorphism

Child classes can override methods from the parent class:

class Pet: """ Pet class """ def speak(self): """ Speak method """ print("Some animal is speaking") class Cat(Pet): """ Cat class, inherits from Pet """ def speak(self): """ Speak method """ print("Meow Meow Meow") class Dog(Pet): """ Dog class, inherits from Pet """ def speak(self): """ Speak method """ print("Woof Woof Woof") class Rabbit(Pet): """ Rabbit class, inherits from Pet """ pass # Create objects cat = Cat() dog = Dog() rabbit = Rabbit() # Call speak method cat.speak() # Calls overridden speak in Cat dog.speak() # Calls overridden speak in Dog rabbit.speak() # Rabbit does not override speak, calls Pet's speak # Petting a pet def fondle(pet: Pet): pet.speak() # Calls speak based on the actual type # Pet the animals fondle(cat) fondle(dog) fondle(rabbit)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

In this example, the function fondle takes a parameter pet of base class type Pet. When calling pet.speak(), it dynamically calls the correct overridden method according to the actual object type.

This behavior — using a unified interface to invoke different behaviors — is called polymorphism.

#super

If a child class overrides the constructor, it needs to call the parent class's constructor within it to initialize inherited attributes. In Python, you use the super() function to access the parent class.

class Pet: """ Pet class """ def __init__(self, name: str): """ Constructor Args: name (str): Name """ self.name = name class Cat(Pet): """ Cat class, inherits from Pet """ def __init__(self, name: str, breed: str): """ Constructor Args: name (str): Name breed (str): Breed """ super().__init__(name) # Call parent constructor self.breed = breed # New attribute # Create object cat = Cat("Tom", "Chartreux") print(cat.name) print(cat.breed)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Created in 5/15/2025

Updated in 5/21/2025